package org.anyin.gitee.cloud.center.upms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.anyin.gitee.cloud.center.upms.controller.form.SysRolePageForm;
import org.anyin.gitee.cloud.center.upms.controller.form.SysRoleSaveForm;
import org.anyin.gitee.cloud.center.upms.convert.SysRoleConvert;
import org.anyin.gitee.cloud.center.upms.service.SysRoleService;
import org.anyin.gitee.cloud.center.upms.service.dto.SysRoleInfoDTO;
import org.anyin.gitee.cloud.center.upms.service.dto.SysRoleSaveDTO;
import org.anyin.gitee.cloud.lib.core.web.ApiResponse;
import org.anyin.gitee.cloud.lib.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/sys-role")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleConvert sysRoleConvert;

    @PostMapping("/create")
    public ApiResponse create(@Valid @RequestBody SysRoleSaveForm form){
        SysRoleSaveDTO param = sysRoleConvert.getSysRoleSaveDTO(form);
        sysRoleService.create(param);
        return ApiResponse.success();
    }

    @GetMapping("/info/{id}")
    public ApiResponse info(@PathVariable("id") Long id){
        SysRoleInfoDTO info = sysRoleService.info(id);
        return ApiResponse.success(info);
    }

    @PostMapping("/modify/{id}")
    public ApiResponse modify(@PathVariable("id") Long id, @Valid @RequestBody SysRoleSaveForm form){
        SysRoleSaveDTO param = sysRoleConvert.getSysRoleSaveDTO(form);
        sysRoleService.modify(id, param);
        return ApiResponse.success();
    }

    @PostMapping("/delete/{id}")
    public ApiResponse delete(@PathVariable("id") Long id){
        sysRoleService.delete(id);
        return ApiResponse.success();
    }

    @PostMapping("/page")
    public ApiResponse page(@Valid @RequestBody SysRolePageForm form){
        Page<SysRoleInfoDTO> page = sysRoleService.page(form.getPageNo(), form.getPageSize(), form.getRoleName());
        return ApiResponse.success(page);
    }

    @PostMapping("/auth/{id}")
    public ApiResponse auth(@PathVariable("id") Long id, @RequestBody List<Long> menuIds){
        sysRoleService.auth(id, menuIds);
        return ApiResponse.success();
    }
}
